package com.jack.lib.core.ext

import java.math.BigDecimal

/**
 * author : jack(黄冲)
 * e-mail : 907755845@qq.com
 * create : 2020/7/4
 * desc   :
 */

fun Number?.toIntOrDef(def: Int): Int = kotlin.runCatching { this?.toInt()?: 0 }.getOrElse { def }
fun Number?.toFloatOrDef(def: Float): Float = kotlin.runCatching { this?.toFloat()?: 0f }.getOrElse { def }
fun Number?.toDoubleOrDef(def: Double): Double = kotlin.runCatching { this?.toDouble()?: 0.0 }.getOrElse { def }

fun String?.toIntOrDef(def: Int): Int = kotlin.runCatching { this?.toInt()?: 0 }.getOrElse { def }
fun String?.toFloatOrDef(def: Float): Float = kotlin.runCatching { this?.toFloat()?: 0f }.getOrElse { def }
fun String?.toDoubleOrDef(def: Double): Double = kotlin.runCatching { this?.toDouble()?: 0.0 }.getOrElse { def }


fun String?.toDoubleOr0(): Double {
    if (this.isNullOrEmpty()) {
        return 0.0
    }
    return this.toDouble()
}


fun String?.toIntOr0(): Int = kotlin.runCatching { this!!.toInt() }.getOrElse { 0 }

fun String?.toNumberStr(): String{
    if (this.isNullOrEmpty()){
        return "0"
    }
    return this.replace(",", "")
}

fun Number?.toNumberStr(): String{
    if (this == null){
        return "0"
    }
    return this.toString().replace(",", "")
}

fun String?.multiply(value: Number?, newScale: Int = 2): String{
    val big1 = BigDecimal(toNumberStr())
    val big2 = BigDecimal(value.toNumberStr())
    return big1.multiply(big2).setScale(newScale, BigDecimal.ROUND_UP).toString()
}

fun String?.multiply(value: String?, newScale: Int = 2): String{
    val big1 = BigDecimal(toNumberStr())
    val big2 = BigDecimal(value.toNumberStr())
    return big1.multiply(big2).setScale(newScale, BigDecimal.ROUND_UP).toString()
}

fun String?.add(value: Number?, newScale: Int = 2): String{
    val big1 = BigDecimal(toNumberStr())
    val big2 = BigDecimal(value.toNumberStr())
    return big1.add(big2).setScale(newScale, BigDecimal.ROUND_UP).toString()
}

fun String?.add(value: String?, newScale: Int = 2): String{
    val big1 = BigDecimal(toNumberStr())
    val big2 = BigDecimal(value.toNumberStr())
    return big1.add(big2).setScale(newScale, BigDecimal.ROUND_UP).toString()
}

fun String?.subtract(value: Number?, newScale: Int = 2): String{
    val big1 = BigDecimal(toNumberStr())
    val big2 = BigDecimal(value.toNumberStr())
    return big1.subtract(big2).setScale(newScale, BigDecimal.ROUND_UP).toString()
}

fun String?.subtract(value: String?, newScale: Int = 2): String{
    val big1 = BigDecimal(toNumberStr())
    val big2 = BigDecimal(value.toNumberStr())
    return big1.subtract(big2).setScale(newScale, BigDecimal.ROUND_UP).toString()
}

fun String?.divide(value: Number?, newScale: Int = 2): String{
    val big1 = BigDecimal(toNumberStr())
    val big2 = BigDecimal(value.toNumberStr())
    return big1.divide(big2, newScale, BigDecimal.ROUND_UP).toString()
}

fun String?.divide(value: String?, newScale: Int = 2): String{
    val big1 = BigDecimal(toNumberStr())
    val big2 = BigDecimal(value.toNumberStr())
    return big1.divide(big2, newScale, BigDecimal.ROUND_UP).toString()
}

fun String?.isEqualTo(value: String?): Boolean {
    return this.toBigDecimalNotNull().compareTo(value.toBigDecimalNotNull()) == 0
}

fun String?.isGreaterThan(value: String?): Boolean {
    return this.toBigDecimalNotNull().compareTo(value.toBigDecimalNotNull()) == 1
}

fun String?.isLessThan(value: String?): Boolean {
    return this.toBigDecimalNotNull().compareTo(value.toBigDecimalNotNull()) == -1
}

fun String?.containNumber(): Boolean {
    for (ch in this?: return false) {
        if (Character.isDigit(ch)) {
            return true
        }
    }
    return false
}

fun String?.containLetter(): Boolean {
    for (ch in this?: return false) {
        if (Character.isLetter(ch)) {
            return true
        }
    }
    return false
}


fun String?.toBigDecimalNotNull(): BigDecimal{
    if (this.isNullOrEmpty()){
        return BigDecimal("0")
    }
    return this.toBigDecimal()
}


